﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Common;
using Model;
using Model.DtoModel;
using Model.EasyUiModel;
using Model.ViewModel;
using MvcOa.Helper;

namespace MvcOa.Logic.Admin
{
    public class RoleController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult GetRoleList()
        {
            int pageSize = RequestHelper.GetInt("rows");
            int pageIndex = RequestHelper.GetInt("page");
            int count = 0;
            List<RoleViewModel> roles = OperationContext.Current.IBllSession.IRoleBll
                .GetPagedList(pageIndex, pageSize, w => w.rIsDel == false, o => o.rId, out count)
                .Select(m => new RoleViewModel()
                {
                    rAddTime = m.rAddTime,
                    rDepId = m.rDepId,
                    rDepName = m.Department.depName,
                    rId = m.rId,
                    rIsDel = m.rIsDel,
                    rIsShow = m.rIsShow,
                    rName = m.rName,
                    rRemark = m.rRemark
                }).ToList();
            DataGridModel dataGridModel = new DataGridModel()
            {
                total = count,
                footer = null,
                rows = roles
            };
            return Json(dataGridModel);
        }

        public ActionResult Add()
        {
            BuildSelectList();
            return PartialView(new RoleViewModel());
        }

        private void BuildSelectList()
        {
            ViewBag.DepartmentList = OperationContext.Current.IBllSession.IDepartmentBll.GetAll()
                .ToList().Select(m => new SelectListItem() {Text = m.depName, Value = m.depId.ToString()}).ToList();
        }

        [HttpPost]
        public ActionResult Add(RoleViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                if (viewModel != null)
                {
                    Role role=new Role()
                    {
                        rAddTime = DateTime.Now,
                        rDepId = viewModel.rDepId,
                        rIsDel = false,
                        rIsShow = true,
                        rName = viewModel.rName,
                        rRemark = viewModel.rRemark
                    };
                    int n = OperationContext.Current.IBllSession.IRoleBll.Add(role);

                    return Redirect("~/Admin/System/Permission");
                }
            }
            LogHelper.WriteLog(typeof(SystemController), "AddPermission");
            return View("OperationError"); ;
        }

        public ActionResult Edit(int id)
        {
            RoleViewModel viewModel =
                OperationContext.Current.IBllSession.IRoleBll.GetListBy(m => m.rId == id)
                    .Select(m => new RoleViewModel()
                    {
                        rId = m.rId,
                        rName = m.rName,
                        rRemark = m.rRemark,
                        rDepId = m.rDepId
                    }).FirstOrDefault();
            BuildSelectList();
            return PartialView("Edit", viewModel);
        }

        [HttpPost]
        public ActionResult Edit(RoleViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                if (viewModel != null && viewModel.rId != 0)
                {
                    Role role=new Role()
                    {
                        rId = viewModel.rId,
                        rDepId = viewModel.rDepId,
                        rName = viewModel.rName,
                        rRemark = viewModel.rRemark
                    };
                    int n = OperationContext.Current.IBllSession.IRoleBll
                        .Modify(role, "rDepId", "rName", "rRemark");
                    return Redirect("~/Admin/Role/Index");
                }
            }
            LogHelper.WriteLog(typeof(RoleController), "EditRole");
            return View("OperationError"); ;
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            MessageModel messageModel = new MessageModel();
            if (!OperationContext.Current.IBllSession.IRoleBll.GetListBy(m => m.rId == id).Any())
            {
                //主外键约束，不能删除角色
                messageModel.IsSuccess = false;
                messageModel.Message = "不存在该角色，不能删除！";
                return Json(messageModel);
            }
            try
            {
                int n = OperationContext.Current
                .IBllSession.IRoleBll.DeleteBy(m => m.rId == id);
                if (n <= 0)
                {
                    messageModel.IsSuccess = false;
                    messageModel.Message = "删除失败!";
                }
            }
            catch (Exception e)
            {
                messageModel.IsSuccess = false;
                messageModel.Message = e.Message;
                throw;
            }
            return Json(messageModel);
        }
    }
}
